package owg.util.awt;

import java.awt.Rectangle;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;

public class StatefulJFrame extends JFrame implements ComponentListener, WindowListener
	{
	private final static boolean DEBUG = false;
	private static final long serialVersionUID = -2107614452602271181L;
	
	public Rectangle windowedBounds = null;
	public boolean isFullscreen = false;
	
	public StatefulJFrame(String name)
		{
		super(name);
		this.addComponentListener(this);
		this.addWindowListener(this);
		}
	
	public boolean isFullscreen()
		{
		return isFullscreen;
		}
	public boolean isIconified()
		{
		return (getExtendedState()&ICONIFIED) != 0;
		}
	
	public Rectangle getWindowedBounds()
		{
		return windowedBounds;
		}
	
	private void updateState()
		{
		if(!isIconified())
			{
			int es = getExtendedState();
			if((es & MAXIMIZED_BOTH )!= 0)
				isFullscreen = true;
			else
				{
				windowedBounds = getBounds();
				isFullscreen = false;
				}
			}
		}

	@Override
	public void componentHidden(ComponentEvent e)
		{
		if(DEBUG) System.out.println(e);
		}

	@Override
	public void componentMoved(ComponentEvent e)
		{
		if(DEBUG) 
			{
			System.out.println(e);
			System.out.println(Integer.toBinaryString(getExtendedState()));
			}
		updateState();
		}

	@Override
	public void componentResized(ComponentEvent e)
		{
		if(DEBUG) 
			{
			System.out.println(e);
			System.out.println(Integer.toBinaryString(getExtendedState()));
			}
		updateState();
		}

	@Override
	public void componentShown(ComponentEvent e)
		{
		if(DEBUG) System.out.println(e);
		
		updateState();
		}

	@Override
	public void windowActivated(WindowEvent e)
		{
		if(DEBUG) System.out.println(e);
		}

	@Override
	public void windowClosed(WindowEvent e)
		{
		if(DEBUG) System.out.println(e);
		}

	@Override
	public void windowClosing(WindowEvent e)
		{
		if(DEBUG) System.out.println(e);
		}

	@Override
	public void windowDeactivated(WindowEvent e)
		{
		if(DEBUG) 
			{
			System.out.println(e);
			System.out.println(Integer.toBinaryString(getExtendedState()));
			}
		}

	@Override
	public void windowDeiconified(WindowEvent e)
		{
		if(DEBUG) 
			{
			System.out.println(e);
			System.out.println(Integer.toBinaryString(getExtendedState()));
			}
		updateState();
		}

	@Override
	public void windowIconified(WindowEvent e)
		{
		if(DEBUG) System.out.println(e);
		}

	@Override
	public void windowOpened(WindowEvent e)
		{
		if(DEBUG) System.out.println(e);
		}
	}
